package com.rtx.edi.platform.application.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.application.dto.PlAppEnSeLiDTO;
import com.rtx.edi.model.application.po.PlApplicationEnvironment;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.application.service.PlApplicationEnvironmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 应用环境信息表 前端控制器
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-06
 */
@Api(tags = {"应用环境信息管理"})
@RestController
@RequestMapping("/v1/plApplicationEnvironment")
public class PlApplicationEnvironmentController {

	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PlApplicationEnvironmentService plApplicationEnvironmentService;

	/**
	 * 获取应用环境信息列表
	 * 
	 * @return
	 */
	@PostMapping(value = "/selectList", produces = "application/json; charset=utf-8")
	public EdiResponse selectPlApplicationEnvironmentList(@RequestBody PlAppEnSeLiDTO dto) {
		int page = dto.getPageNum();
		int size = dto.getPageSize();
		EdiResponse ediResponse = new EdiResponse();
		try {
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession();
			String companyid = (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
			dto.setCompanyId(Long.parseLong(companyid));
			
			PlApplicationEnvironment queryCondition = new PlApplicationEnvironment();
			BeanUtils.copyProperties(dto, queryCondition);
			Page<PlApplicationEnvironment> pageParam = new Page<>(page, size);
			plApplicationEnvironmentService.selectPlApplicationEnvironmentList(pageParam, queryCondition);

			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("page", pageParam);
			ediResponse.setData(dataMap);
			ediResponse.setReturnCode(200);
			ediResponse.setReturnMgr("success");

		} catch (Exception e) {
			ediResponse.setReturnCode(400);
			ediResponse.setReturnMgr(e.getMessage());
			log.error("新增应用环境信息异常");
		}
		return ediResponse;
	}

	/**
	 * 根据ID获取应用环境信息
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/selectbyid")
	public EdiResponse selectPlApplicationEnvironmentById(@RequestBody PlAppEnSeLiDTO dto) {
		EdiResponse ediResponse = new EdiResponse();
		try {
			PlApplicationEnvironment plApplicationEnvironment = Optional .ofNullable(plApplicationEnvironmentService.getById(dto.getId())).orElse(new PlApplicationEnvironment());

			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("item", plApplicationEnvironment);
			ediResponse.setData(dataMap);
			ediResponse.setReturnCode(200);
			ediResponse.setReturnMgr("success");
		} catch (Exception e) {
			ediResponse.setReturnCode(400);
			ediResponse.setReturnMgr(e.getMessage());
			log.error("根据ID获取应用环境信息-", e);
		}
		return ediResponse;
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除应用环境信息", notes = "根据id删除应用环境信息")
	@PostMapping("/deletebyid")
	public EdiResponse deletePlApplicationEnvironmentById(@RequestBody PlAppEnSeLiDTO dto) {
		EdiResponse ediResponse = new EdiResponse();
		try {
			plApplicationEnvironmentService.delPlApplicationEnvironment(dto.getId());
			ediResponse.setReturnCode(200);
			ediResponse.setReturnMgr("success");
		} catch (Exception e) {
			log.error("删除应用环境信息异常-", e);
			ediResponse.setReturnCode(400);
			ediResponse.setReturnMgr(e.getMessage());
		}
		return ediResponse;
	}
	
	
	
	@ApiOperation(value = "批量删除应用环境信息", notes = "根据id列表删除应用环境信息")
	@PostMapping("/deletebyids")
	public EdiResponse deletePlApplicationEnvironmentByIds(@RequestBody List<PlAppEnSeLiDTO> list) {
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
		return plApplicationEnvironmentService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
	}

	/**
	 * 新增应用环境信息
	 * 
	 * @param PlApplicationEnvironment
	 * @return
	 */
	@ApiOperation(value = "新增应用环境信息")
	@PostMapping(value = "/create")
	public EdiResponse addPlApplicationEnvironment(@RequestBody PlAppEnSeLiDTO dto) {
		EdiResponse ediResponse = new EdiResponse();
		try {
			
			PlApplicationEnvironment po = new PlApplicationEnvironment();
			BeanUtils.copyProperties(dto, po);
			LambdaQueryWrapper<PlApplicationEnvironment> qw = Wrappers.lambdaQuery();
			qw.eq(PlApplicationEnvironment::getEnvironmentName, po.getEnvironmentName());
			boolean exists = plApplicationEnvironmentService.getBaseMapper().exists(qw);
			if(exists) {
				ediResponse.setReturnCode(400);
				ediResponse.setReturnMgr("应用环境名称已存在不允许重复创建！");
			}else {
				plApplicationEnvironmentService.save(po);
				ediResponse.setReturnCode(200);
				ediResponse.setReturnMgr("success");
			}
		} catch (Exception e) {
			log.error("新增应用环境信息异常");
			ediResponse.setReturnCode(400);
			ediResponse.setReturnMgr(e.getMessage());
		}
		return ediResponse;
	}

	/**
	 * 根据ID修改应用环境信息
	 * 
	 * @return
	 */
	@ApiOperation(value = "根据ID修改应用环境信息")
	@PostMapping(value = "/modidy")
	public EdiResponse editPlApplicationEnvironmentById(@RequestBody PlAppEnSeLiDTO dto) {
		EdiResponse ediResponse = new EdiResponse();
		try {
			PlApplicationEnvironment po = new PlApplicationEnvironment();
			BeanUtils.copyProperties(dto, po);
			plApplicationEnvironmentService.updateById(po);
			ediResponse.setReturnCode(200);
			ediResponse.setReturnMgr("success");
		} catch (Exception e) {
			log.error("根据ID修改应用环境信息异常-", e);
			ediResponse.setReturnCode(400);
			ediResponse.setReturnMgr(e.getMessage());
		}
		return ediResponse;
	}
}
